﻿using Autofac;

namespace Common.Core.IoC
{
    /// <summary>
    /// Autofac IOC 容器
    /// </summary>
    public class IoCContainer
    {
        private static ILifetimeScope _container;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static void InitContainer(ILifetimeScope autofacContainer)
        {
            _container = autofacContainer;
        }

        /// <summary>
        /// 从容器中获取对象 Resolve an instance of the default requested type from the container
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns></returns>
        public static T Resolve<T>()
        {
            return _container.Resolve<T>();
        }

        /// <summary>
        /// 从容器中获取对象 Resolve an instance of the default requested type from the container
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns></returns>
        public static object ResolveOptional(Type serviceType)
        {
            return _container.ResolveOptional(serviceType);
        }
    }
}
